import os
import json
import yaml
import pathlib
import typing


def read(path: str | pathlib.Path, mode: typing.Literal["r", "rb"] = "r"):
    with open(path, mode) as file:
        return file.read()


def write(path: str | pathlib.Path, data: typing.Any, mode: typing.Literal["w", "wb", "a"] = "a"):
    with open(path, mode) as file:
        return file.write(data)


def read_json(path: str | pathlib.Path):
    with open(path, "r") as file:
        return json.load(file)


def write_json(path: str | pathlib.Path, data: dict | list):
    with open(path, "w") as file:
        return json.dump(data, file, indent=3, ensure_ascii=False)


def read_yaml(path: str | pathlib.Path):
    with open(path, "r") as file:
        return yaml.safe_load(file)


def write_yaml(path: str | pathlib.Path, data: typing.Any, allow_unicode: bool = True, sort_keys: bool = False):
    with open(path, "w") as file:
        return yaml.dump(data, file, allow_unicode=allow_unicode, sort_keys=sort_keys)
